package com.apache.passport.common;

import com.apache.cache.util.Validator;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.Map;

/**
 * description: sso转换模型
 *
 * @author dyh 创建时间：2019年03月03日
 */
@XmlRootElement(name = "ConversionModel")
public class SsoConversionModel {

    private String userEname;

    private String userCname;

    private String userPass;

    private String userStatus;

    private String email;

    private String mobile;

    private String regIp;

    private String delStatus;

    private String userType;

    private String userLevel;

    private String customerType;

    private String tableName;

    private String enableConfig;
    private String jdbcUrl;
    private String jdbcDeriver;
    private String jdbcUser;
    private String jdbcPwd;

    public String getUserEname() {
        return userEname;
    }

    public void setUserEname(String userEname) {
        this.userEname = userEname;
    }

    public String getUserCname() {
        return userCname;
    }

    public void setUserCname(String userCname) {
        this.userCname = userCname;
    }

    public String getUserPass() {
        return userPass;
    }

    public void setUserPass(String userPass) {
        this.userPass = userPass;
    }

    public String getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(String userStatus) {
        this.userStatus = userStatus;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getRegIp() {
        return regIp;
    }

    public void setRegIp(String regIp) {
        this.regIp = regIp;
    }

    public String getDelStatus() {
        return delStatus;
    }

    public void setDelStatus(String delStatus) {
        this.delStatus = delStatus;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public String getUserLevel() {
        return userLevel;
    }

    public void setUserLevel(String userLevel) {
        this.userLevel = userLevel;
    }

    public String getCustomerType() {
        return customerType;
    }

    public void setCustomerType(String customerType) {
        this.customerType = customerType;
    }

    public String getEnableConfig() {
        return enableConfig;
    }

    public void setEnableConfig(String enableConfig) {
        this.enableConfig = enableConfig;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getJdbcDeriver() {
        return jdbcDeriver;
    }

    public void setJdbcDeriver(String jdbcDeriver) {
        this.jdbcDeriver = jdbcDeriver;
    }

    public String getJdbcUser() {
        return jdbcUser;
    }

    public void setJdbcUser(String jdbcUser) {
        this.jdbcUser = jdbcUser;
    }

    public String getJdbcPwd() {
        return jdbcPwd;
    }

    public void setJdbcPwd(String jdbcPwd) {
        this.jdbcPwd = jdbcPwd;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    /*public String insertUctUser(Map<String,String> params){
        StringBuilder sb = new StringBuilder();
        StringBuilder vals = new StringBuilder("values(");
        sb.append("INSERT INTO "+tableName+"( ");
        sb.append(userEname+","+userCname+",");
        vals.append("'"+params.get("userEname")+"',");
        vals.append("'"+params.get("userCname")+"',");
        if(Validator.isNotNull(email)){
            sb.append(email+",");
            vals.append("'"+params.get("email")+"',");
        }
        if(Validator.isNotNull(mobile)){
            sb.append(mobile+",");
            vals.append("'"+params.get("mobile")+"',");
        }
        if(Validator.isNotNull(userStatus)){
            sb.append(userStatus+",");
            vals.append("'"+params.get("userStatus")+"',");
        }
        if(Validator.isNull(regIp)){
            sb.append(regIp+",");
            vals.append("'"+params.get("regIp")+"',");
        }
        if(Validator.isNull(delStatus)){
            sb.append(delStatus+",");
            vals.append("'"+params.get("delStatus")+"',");
        }
        if(Validator.isNotNull(userType)){
            sb.append(userType+",");
            vals.append("'"+params.get("userType")+"',");
        }
        if(Validator.isNotNull(userLevel)){
            sb.append(userLevel+" ,");
            vals.append("'"+params.get("userLevel")+"',");
        }
        if(Validator.isNotNull(customerType)){
            sb.append(customerType+",");
            vals.append("'"+params.get("customerType")+"',");
        }
        sb.append(userPass+")");
        vals.append("'"+params.get("userPass")+"')");
        sb.append(vals.toString());
        return sb.toString();
    }*/

    public String sqlByProperty(Map<String,String> params){
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append(selectStr());
        sb.append(" from "+tableName+" ");
        sb.append(whereStr(params));
        return sb.toString();
    }

    public String sqlCountByProperty(Map<String,String> params){
        StringBuilder sb = new StringBuilder();
        sb.append("select count(1)");
        sb.append(" from "+tableName+" ");
        sb.append(whereStr(params));
        return sb.toString();
    }

    public String sqlByUserEname(String userEname,boolean mark){
        StringBuilder sb = new StringBuilder();
        if(Validator.isNotNull(userEname)){
            sb.append("select ");
            sb.append(selectStr());
            sb.append(" from "+tableName+" where ");
            sb.append(this.userEname+"='"+userEname+"'");
            if(mark) {
                if (Validator.isNotNull(email)) {
                    sb.append(" or " + email + "='" + userEname + "'");
                }
                if (Validator.isNotNull(mobile)) {
                    sb.append(" or " + mobile + "='" + userEname + "'");
                }
            }
        }
        return sb.toString();
    }

    public String sqlByEmail(String email){
        StringBuilder sb = new StringBuilder();
        if(Validator.isNotNull(email)){
            sb.append("select ");
            sb.append(selectStr());
            sb.append(" from "+tableName+" where ");
            if(Validator.isNotNull(this.email)){
                sb.append(this.email+"='"+email+"'");
            }
        }
        return sb.toString();
    }

    public String sqlByMobile(String mobile){
        StringBuilder sb = new StringBuilder();
        if(Validator.isNotNull(mobile)){
            sb.append("select ");
            sb.append(selectStr());
            sb.append(" from "+tableName+" where ");
            if(Validator.isNotNull(this.mobile)){
                sb.append(this.mobile+"='"+mobile+"'");
            }
        }
        return sb.toString();
    }

    private String selectStr(){
        StringBuilder sb = new StringBuilder();
        sb.append(userEname+" user_ename,");
        sb.append(userCname+" user_cname,");
        if(Validator.isNotNull(email)){
            sb.append(email+" email,");
        }
        if(Validator.isNotNull(mobile)){
            sb.append(mobile+" mobile,");
        }
        if(Validator.isNull(userStatus)){
            sb.append("'1' user_status,");
        }else{
            sb.append(userStatus+" user_status,");
        }
        if(Validator.isNull(regIp)){
            sb.append("'127.0.0.1' reg_ip,");
        }else{
            sb.append(regIp+" reg_ip,");
        }
        if(Validator.isNull(delStatus)){
            sb.append("'F' del_status,");
        }else{
            sb.append(delStatus+" del_status,");
        }
        if(Validator.isNotNull(userType)){
            sb.append(userType+" user_type,");
        }
        if(Validator.isNotNull(userLevel)){
            sb.append(userLevel+" user_level,");
        }
        if(Validator.isNotNull(customerType)){
            sb.append(customerType+" customer_type,");
        }
        sb.append(userPass+" user_pass");
        return sb.toString();
    }

    private String whereStr(Map<String,String> params){
        StringBuilder sb = new StringBuilder(" where 1=1 ");
        if(Validator.isNotNull(params.get("userEname"))){
            sb.append(" and "+userEname+"='"+params.get("userEname")+"'");
        }
        if(Validator.isNotNull(params.get("email")) && Validator.isNotNull(email)){
            sb.append(" and "+email+"='"+params.get("email")+"'");
        }
        if(Validator.isNotNull(params.get("mobile")) && Validator.isNotNull(mobile)){
            sb.append(" and "+mobile+"='"+params.get("mobile")+"'");
        }
//        if(Validator.isNotNull(params.get("orgEname"))){
//            sb.append(" and "+orgEname+"='"+params.get("orgEname")+"'");
//        }
        return sb.toString();
    }
}
